package benny.ApkposStore.model;
import com.tencent.elife.asyn.BaseManager;
import com.tencent.elife.asyn.DataRequest;
import com.tencent.elife.asyn.DataResponse;
import com.tencent.elife.net.NetUtils;
import com.tencent.elife.utils.AppUtils;

import android.content.Context;
import com.tencent.elife.cache.CacheManager;
import benny.ApkposStore.proto.Basic.BasicResponse;
import benny.ApkposStore.proto.Basic.BasicResponse12;
import benny.ApkposStore.proto.Basic.BasicResponse19;
import benny.ApkposStore.proto.Basic.BasicResponse20;
import benny.ApkposStore.proto.Basic.BasicResponse3;
import benny.ApkposStore.proto.Basic.BasicResponse6;
import benny.ApkposStore.proto.Basic.BasicResponse8;
import benny.ApkposStore.proto.Basic.BasicResponse9;
import benny.ApkposStore.proto.Room.RoomResponse;


//
//第一次获取instance的时候，需要进行init，init是从BaseManager继承过来的方法
//
public class DataManager extends BaseManager {
	
	private static DataManager ins_ = null;// the only instance in App 
	private Context ctx_ = null;
	private static String cacheDir_ = "apkpos/cache";
	
	private static long basiceCacheDuration_ = 0; //milliseconds
	
	
	public static DataManager instance()
	{
		if(ins_ == null) {
			ins_ = new DataManager();
		}
		return ins_;
	}
	
	public void init(Context appCtx) {
		ctx_ = appCtx;
		
		super.init(ctx_);		
		NetUtils.init(ctx_);		
		CacheManager.init(ctx_, cacheDir_);
	}
	
	
	public void release()
	{
		super.release();
		ins_ = null;
	}
	
	
	public void login() {
		
		//addTask(DataRequest<T> request, DataResponse<T> response);
		
	}
	
	//查询基础信息
	public void queryBasic(final DataResponse<BasicResponse> response) {
		
		addTask(new DataRequest<BasicResponse>(){
			@Override
			public BasicResponse doInBackGround() throws Exception {
				String url = "http://58.215.189.116/wsgi/query_basic";
				String cacheIndetify = AppUtils.toMD5(url);			
				String param = "";
				byte[]  data = CacheManager.fetchByteArrayFromPost(url, param, cacheIndetify, basiceCacheDuration_);				
				return BasicResponse.parseFrom(data);
			}
		}, response);
	}
		
	//查询房台信息
	public void queryRoom(final DataResponse<RoomResponse> response) {
		addTask(new DataRequest<RoomResponse>(){
			@Override
			public RoomResponse doInBackGround() throws Exception {
				String url = "http://58.215.189.116/wsgi/query_room";
				String cacheIndetify = AppUtils.toMD5(url);		
				String param = "";
				byte[]  data = CacheManager.fetchByteArrayFromPost(url, param, cacheIndetify, basiceCacheDuration_);				
				return RoomResponse.parseFrom(data);
			}
		}, response);		
	}

	public void basiccai_name(DataResponse<BasicResponse6> response) {
		// TODO Auto-generated method stub
		addTask(new DataRequest<BasicResponse6>(){
			@Override
			public BasicResponse6 doInBackGround() throws Exception {
				String url = "http://58.215.189.116/wsgi/basiccai_name";
				String cacheIndetify = AppUtils.toMD5(url);			
				String param = "";
				byte[]  data = CacheManager.fetchByteArrayFromPost(url, param, cacheIndetify, basiceCacheDuration_);				
				return BasicResponse6.parseFrom(data);
			}
		}, response);	
	}
	public void basicfloor_tables(DataResponse<BasicResponse8> response) {
		// TODO Auto-generated method stub
		addTask(new DataRequest<BasicResponse8>(){
			@Override
			public BasicResponse8 doInBackGround() throws Exception {
				String url = "http://58.215.189.116/wsgi/basicfloor_tables";
				String cacheIndetify = AppUtils.toMD5(url);			
				String param = "";
				byte[]  data = CacheManager.fetchByteArrayFromPost(url, param, cacheIndetify, basiceCacheDuration_);				
				return BasicResponse8.parseFrom(data);
			}
		}, response);	
	}
	public void basicroom_tables(DataResponse<BasicResponse12> response) {
		// TODO Auto-generated method stub
		addTask(new DataRequest<BasicResponse12>(){
			@Override
			public BasicResponse12 doInBackGround() throws Exception {
				String url = "http://58.215.189.116/wsgi/basicroom_tables";
				String cacheIndetify = AppUtils.toMD5(url);			
				String param = "";
				byte[]  data = CacheManager.fetchByteArrayFromPost(url, param, cacheIndetify, basiceCacheDuration_);				
				return BasicResponse12.parseFrom(data);
			}
		}, response);	
	}
	public void basiccai_class(DataResponse<BasicResponse3> response) {
		// TODO Auto-generated method stub
		addTask(new DataRequest<BasicResponse3>(){
			@Override
			public BasicResponse3 doInBackGround() throws Exception {
				String url = "http://58.215.189.116/wsgi/basiccai_class";
				String cacheIndetify = AppUtils.toMD5(url);			
				String param = "";
				byte[]  data = CacheManager.fetchByteArrayFromPost(url, param, cacheIndetify, basiceCacheDuration_);				
				return BasicResponse3.parseFrom(data);
			}
		}, response);	
	}
	public void basiccai_classfather(DataResponse<BasicResponse19> response) {
		// TODO Auto-generated method stub
		addTask(new DataRequest<BasicResponse19>(){
			@Override
			public BasicResponse19 doInBackGround() throws Exception {
				String url = "http://58.215.189.116/wsgi/basiccai_classfather";
				String cacheIndetify = AppUtils.toMD5(url);			
				String param = "";
				byte[]  data = CacheManager.fetchByteArrayFromPost(url, param, cacheIndetify, basiceCacheDuration_);				
				return BasicResponse19.parseFrom(data);
			}
		}, response);	
	}
	
	public void basicmake_tbl(DataResponse<BasicResponse9> response) {
		// TODO Auto-generated method stub
		addTask(new DataRequest<BasicResponse9>(){
			@Override
			public BasicResponse9 doInBackGround() throws Exception {
				String url = "http://58.215.189.116/wsgi/basicmake_tbl";
				String cacheIndetify = AppUtils.toMD5(url);			
				String param = "";
				byte[]  data = CacheManager.fetchByteArrayFromPost(url, param, cacheIndetify, basiceCacheDuration_);				
				return BasicResponse9.parseFrom(data);
			}
		}, response);	
	}
	
	public void basicview_dian(DataResponse<BasicResponse20> response) {
		// TODO Auto-generated method stub
		addTask(new DataRequest<BasicResponse20>(){
			@Override
			public BasicResponse20 doInBackGround() throws Exception {
				String url = "http://58.215.189.116/wsgi/basicview_dian";
				String cacheIndetify = AppUtils.toMD5(url);			
				String param = "";
				byte[]  data = CacheManager.fetchByteArrayFromPost(url, param, cacheIndetify, basiceCacheDuration_);				
				return BasicResponse20.parseFrom(data);
			}
		}, response);	
	}
	
}
